package com.tuanzi.loan.business.service.schedule;

import com.tuanzi.loan.business.constants.settlement.CompanyCooperationMode;
import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.entity.settlement.SettlementPriceChangeRecord;
import com.tuanzi.loan.business.repository.settlement.SettlementPriceChangeRecordRepository;
import com.tuanzi.loan.business.service.settlement.CompanyInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 结算价格生效定时器
 * @author caiwanlin
 * @create 2018-01-24 15:03
 */
@Debug
@Log4j
@Service
@Transactional
public class SettlementPriceScheduler {
    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private SettlementPriceChangeRecordRepository settlementPriceChangeRecordRepository;
    @Scheduled(cron = "${cron.settlement_price_effective_task}")
    public void effectivePrice(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        /* try {
            calendar.setTime(DateUtil.getDate("2018-01-26","yyyy-MM-dd"));
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        List<SettlementPriceChangeRecord> records = settlementPriceChangeRecordRepository.findAllByEffectiveTime(calendar.getTime());
        records.forEach(record -> {
            CompanyInfo companyInfo = companyInfoService.findOne(record.getCompanyId());
            //新的合作模式
            Integer newCopMode = record.getNewCopMode();
            //旧的合作模式
            Integer copMode = record.getCopMode();
            if (newCopMode != null && newCopMode != copMode){
                copMode = newCopMode;
            }
            if(copMode == CompanyCooperationMode.CPA.getIndex()){
                companyInfo.setSettlementPriceCpa(record.getNewYuanPrice() == null ?
                        record.getOldYuanPrice():record.getNewYuanPrice());
                companyInfo.setSettlementPriceCps(null);
            }
            else if (copMode == CompanyCooperationMode.CPS.getIndex()){
                companyInfo.setSettlementPriceCps(record.getNewPercentPrice() == null ?
                        record.getOldPercentPrice():record.getNewPercentPrice());
                companyInfo.setSettlementPriceCpa(null);
            }
            else  if(copMode == CompanyCooperationMode.CPC.getIndex()){
                companyInfo.setSettlementPriceCpa(record.getNewYuanPrice() == null ?
                        record.getOldYuanPrice():record.getNewYuanPrice());
                companyInfo.setSettlementPriceCps(null);
            }
            else  if(copMode == CompanyCooperationMode.CPA_CPS.getIndex()){
                companyInfo.setSettlementPriceCpa(record.getNewYuanPrice() == null ?
                        record.getOldYuanPrice():record.getNewYuanPrice());
                companyInfo.setSettlementPriceCps(record.getNewPercentPrice() == null ?
                        record.getOldPercentPrice():record.getNewPercentPrice());
            }
            //更改公司的合作模式
            companyInfo.setCopMode(copMode);
            companyInfoService.saveOrUpdate(companyInfo);
        });
    }
}
